﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports Microsoft.Samples.WindowsPhoneCloud.StorageClient
Imports Microsoft.Phone.Tasks
Imports System.Windows.Threading
Imports System.IO
Imports System.Globalization

Namespace ViewModel

    Public Class UploadPhotoPageViewModel
        Inherits BaseViewModel
        Private ReadOnly blobClient As ICloudBlobClient
        Private ReadOnly dispatcher As Dispatcher

        Private blobNameValue As String
        Private pathValue As Uri
        Private isUploadingValue As Boolean

        Public Sub New()
            Me.New(App.CloudClientFactory.ResolveCloudBlobClient("UploadAndList"), Deployment.Current.Dispatcher)
        End Sub

        Public Sub New(ByVal blobClient As ICloudBlobClient, ByVal dispatcher As Dispatcher)
            Me.blobClient = blobClient
            Me.dispatcher = dispatcher
        End Sub

        Public Property BlobName() As String
            Get
                Return Me.blobNameValue
            End Get

            Set(ByVal value As String)
                If Me.blobNameValue <> value Then
                    Me.blobNameValue = value
                    Me.NotifyPropertyChanged("BlobName")
                End If
            End Set
        End Property

        Public Property Path() As Uri
            Get
                Return Me.pathValue
            End Get

            Set(ByVal value As Uri)
                If Me.pathValue <> value Then
                    Me.pathValue = value
                    Me.NotifyPropertyChanged("Path")
                End If
            End Set
        End Property

        Public Property IsUploading() As Boolean
            Get
                Return Me.isUploadingValue
            End Get

            Set(ByVal value As Boolean)
                If Me.isUploadingValue <> value Then
                    Me.isUploadingValue = value
                    Me.NotifyPropertyChanged("IsUploading")
                End If
            End Set
        End Property

        Public Property PhotoStream() As Stream

        Public Sub LoadPhoto(ByVal photoResult As PhotoResult)
            Me.Path = New Uri(photoResult.OriginalFileName)
            Me.BlobName = String.Format(CultureInfo.InvariantCulture, "Capture-{0}{1}", Date.Now.ToString("yyyyMMddTHHmmss", CultureInfo.InvariantCulture), System.IO.Path.GetExtension(photoResult.OriginalFileName))
            Me.PhotoStream = photoResult.ChosenPhoto
        End Sub

		Public Sub UploadPhoto(ByVal successCallback As Action(Of String), ByVal failureCallback As Action(Of String))
            Me.IsUploading = True
            Me.blobClient.Upload(
                Me.BlobName,
                Me.PhotoStream,
                    Sub(r)
                        Me.dispatcher.BeginInvoke(
                            Sub()
                                Me.IsUploading = False
                                If r.Exception Is Nothing Then
                                    If successCallback IsNot Nothing Then
                                        successCallback.Invoke(String.Format(CultureInfo.InvariantCulture, "Image file {0} successfully uploaded!", Me.BlobName))
                                    End If
                                Else
                                    If failureCallback IsNot Nothing Then
                                        failureCallback.Invoke(String.Format(CultureInfo.InvariantCulture, "Error: {0}", r.Exception.Message))
                                    End If
                                End If
                            End Sub)
                    End Sub)
        End Sub
    End Class
End Namespace